const getUrlParam = (name, h) => {
  var url = decodeURI(location.search); // 获取url中"?"符后的字串
  var theRequest = {};
  if (url.indexOf('?') !== -1) {
    var str = url.substr(1);
    var strs = str.split('&');
    for (var i = 0; i < strs.length; i++) {
      theRequest[strs[i].split('=')[0]] = unescape(strs[i].split('=')[1]);
    }
  }
  return theRequest[name];
}

const getDateNum = function () {
  let myDate = new Date();
  let startTimeStamp = myDate.getFullYear().toString() + ((myDate.getMonth() + 1) < 10 ? '0' + (myDate.getMonth() + 1) : (myDate.getMonth() + 1)) + '01';
  let endTimeStamp = myDate.getFullYear().toString() + ((myDate.getMonth() + 1) < 10 ? '0' + (myDate.getMonth() + 1) : (myDate.getMonth() + 1))
  let lastMonth = myDate.getMonth() + 1;
  myDate.setMonth(lastMonth);
  let nowDate = new Date();
  let dateNum = Math.ceil((myDate.getTime() - nowDate.getTime()) / 24 / 3600 / 1000);
  endTimeStamp += dateNum.toString()
  return [startTimeStamp, dateNum, endTimeStamp]
}

const fullZero = n => n < 0 ? '0' + n : n
const getDate = () => {
  let d = new Date();
  return `${d.getFullYear()}${fullZero(d.getMonth()) + 1}${fullZero(d.getDate())}`
}

const fetchJsonp = function (url, callback) {
  let script = document.createElement('script')
  script.src = url;
  document.body.appendChild(script)
  window.success = function (res) {
    callback(res)
  }
}

export {
  getDateNum,
  getUrlParam,
  getDate,
  fetchJsonp
}
